//日程数据处理
var jsonGrRCList;
var wld;
//领导日程安排日历渲染
var initLdRcap;
var jsonLdRCList;
var laydate;
$(document).ready(function () {
    layui.use(['layer', 'jquery', 'table', 'element', 'laydate', 'carousel'], function () {
        var layer = layui.layer,
            $ = layui.jquery;
        var table = layui.table;
        var element = layui.element;
        laydate = layui.laydate;
        var carousel = layui.carousel;

        //页面一打开就执行弹层
        layer.ready(function () {
            getGrData('init');
            getWldList();

        });

        //个人日程安排日历渲染
        var initGrRcap = laydate.render({
            elem: "#layDate_gr"
            , position: "static"
            , showBottom: false
            , theme: "default"
            , mark: formatJson(jsonGrRCList)
            ,ready: function(value,date){
                bindFu(jsonGrRCList);
            }
            , done: function (value, date) {
               /* var theme = "<ul class='layer_notice'>";
                var count = 1;
                for (var i = 0; i < jsonGrRCList.length; i++) {

                    if (value === jsonGrRCList[i].dateStr) { //点击2017年8月15日，弹出提示语
                        theme += '<li style="text-align: left;"><div class=\'rcdiv\'>' + count + "、" + jsonGrRCList[i].theme + '</div></li>';
                        count++;
                    }
                }
                theme+="</ul>";
                if (theme != "<ul></ul>") {
                    //initGrRcap.hint(theme);
                    //openTip(theme);
                }*/
            }
        });


        //根据code查询个人日程信息
        function getGrData(userCode) {
            $.ajax({
                type: "get",
                url: window.ViewContextPath + "system/layui/getSchedule?userCode=" + userCode,
                async: false,
                dataType: "json",
                success: function (data) {
                    jsonGrRCList = data.data;
                }
            });
        }


        //获取委领导信息
        function getWldList() {
            $.ajax({
                type: "get",
                url: window.ViewContextPath + "system/layui/getWldList",
                async: false,
                dataType: "json",
                success: function (data) {
                    wld = data.data;
                }
            });
        }

        //显示委领导下拉框
        var isLoad = true;
        $("ul#schedule").on("click", "li", function () {
            if (!$(this).hasClass("layui-this"))//重复点击不执行
            {
                var text = $(this).text();
                if (text == '个人') {
                    $("#wldList").hide();
                    $("#wldmore").hide();
                } else {
                    $("#wldList").show();
                    $("#wldmore").show();
                    //渲染委领导下拉框
                    if (wld != "undefined") {
                        /*$.each(wld,function(index,item){
                        $('#wldListOpt').append(new Option(item.text,item.id));//往下拉菜单里添加元素
                    })*/
                        //去掉select,第一次默认
                        $("#wldName").html(wld[0].userName);
                        $("#wldListOpt").html(wld[0].userCode);
                        var userCode = wld[0].userCode;
                        $('#layDate_ld').empty();
                        getLdData(userCode);
                        initLdRc(jsonLdRCList);
                    }
                }
            }
        });


    });

});

function formatJson(jsonData) {
    var json = "{";
    try {
    for (var i = 0; i < jsonData.length; i++) {
        if (i == jsonData.length - 1) {
            json = json + '"' + jsonData[i].dateStr + '"' + ":" + '""'
        } else {
            json = json + '"' + jsonData[i].dateStr + '"' + ":" + '"",'
        }
    }
    }catch (e){

    }
    json = json + "}";
    var jsonPar = JSON.parse(json);
    return jsonPar;
}

function initLdRc(jsonLdRCList) {
    initLdRcap = laydate.render({
        elem: "#layDate_ld"
        , position: "static"
        , showBottom: false
        , theme: "default"
        , mark: formatJson(jsonLdRCList)
        ,ready: function(date){
            bindFu(jsonLdRCList);
           }
        , done: function (value, date) {
            /*var theme = "";
            var count = 1;
            var theme = "<ul class='layer_notice'>";
            for (var i = 0; i < jsonLdRCList.length; i++) {
                if (value === jsonLdRCList[i].dateStr) { //点击2017年8月15日，弹出提示语
                    theme += '<li style="text-align: left;"><div class="rcdiv">' + count + "、" + jsonLdRCList[i].theme + '</div></li>';
                    count++;
                }
            }
            theme+="</ul>";
            if (theme != "<ul></ul>") {
                //initLdRcap.hint(theme);
               // openTip(theme);
                /!*layer.open({
                      type: 1,
                      shade: false,
                      title: false, //不显示标题
                      content: theme //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
                });*!/

            }*/
        }
    });
}

//根据code查询领导日程信息
function getLdData(userCode) {
    $.ajax({
        type: "get",
        url: window.ViewContextPath + "system/layui/getSchedule?userCode=" + userCode,
        async: false,
        dataType: "json",
        success: function (data) {
            jsonLdRCList = data.data;
        }
    });
}

function changeWld(userCode, userName) {
    $('#layDate_ld').empty();
    $("#wldName").html(userName);
    $("#wldListOpt").html(userCode);
    getLdData(userCode);
    initLdRc(jsonLdRCList);

}

function getWlds() {//dashboard打开委领导
    openDiv("350px", "400px", window.ViewContextPath + "system/layui/toWldPage", "委领导");
}
function getParentWlds() {//platDown的iframe需在最外层打开,放弃了，切换点击事件不好加
    parent.openDiv("350px", "400px", window.ViewContextPath + "system/layui/toWldPage", "委领导");
}
//dashboard下弹出
function showMessageInfo(url,title,recId){
    if(url!=null&&url!=""){
    openDiv("800px", "500px",  window.nginxpath+url, title);
    }else{
        openDiv('900px','500px',window.ViewContextPath+'system/messageInfo/loadMessageInfoOne/'+recId,'详情');
    }
}
//iframe情况下弹出
function parentShowMessageInfo(url,title){
    if(url!=null&&url!="") {
        /*parent.openDiv("800px", "500px", window.nginxpath + url, title);*/
        window.open(window.nginxpath + url);
    }
}
function bindFu(jsonRCList) {
    $(".laydate-day-mark").parent().mouseover(function () {
        var value=$(this).attr("lay-ymd");
        var count = 1;
        var theme = "<ul class='layer_notice'>";
        for (var i = 0; i < jsonRCList.length; i++) {
            if (formatDateString(value,"yyyy-MM-dd") ==jsonRCList[i].dateStr) { //点击2017年8月15日，弹出提示语
                theme += '<li style="text-align: left;"><div class="rcdiv">' + count + "、" + jsonRCList[i].theme + '</div></li>';
                count++;
            }
        }
        theme+="</ul>";
        $("#rcInfodiv").html(theme);
        var left = event.clientX;
        var top = event.clientY;
        $("#rcInfodiv").css("left", left+20);
        $("#rcInfodiv").css("top", top);
        if(count>1){
        $("#rcInfodiv").show();}
    });
    $(".laydate-day-mark").parent().mouseout(function () {
        $("#rcInfodiv").hide();
    });
}

